library(qicharts)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggfortify)

# Run chart of 24 random normal variables
tccplot <- tcc(rnorm(24))
tccplot

ggplotly(tccplot) %>% hide_legend()
# Build data frame for example
d <- data.frame(x = rep(1:24, 4),
                mo = (rep(seq(as.Date('2014-1-1'),
                              length.out = 24,
                              by = 'month'),
                          4)),
                n = rbinom(4 * 24, 100, 0.5),
                d = round(runif(4 * 24, 90, 110)),
                g1 = rep(c('a', 'b'), each = 48),
                g2 = rep(c('A', 'B'), each = 24))
# P chart
p <- tcc(n, d, mo, g1 = g1, g2 = g2, breaks = 12, data = d, chart = 'p')
plot(p)

ggplotly(p)
summary(p)
##   g1 g2 breaks n.obs n.useful        cl       lcl       ucl limits.signal
## 1  a  A      1    12       12 0.5292642 0.3464595 0.7120689         FALSE
## 2  a  A      2    12       12 0.4900000 0.3006266 0.6793734         FALSE
## 3  a  B      1    12       12 0.4967051 0.3075495 0.6858607         FALSE
## 4  a  B      2    12       12 0.5033003 0.3140729 0.6925277         FALSE
## 5  b  A      1    12       12 0.5068826 0.4148747 0.5988905         FALSE
## 6  b  A      2    12       12 0.5191667 0.4161645 0.6221689          TRUE
## 7  b  B      1    12       12 0.5214106 0.2941180 0.7487032         FALSE
## 8  b  B      2    12       12 0.5160173 0.3397029 0.6923317         FALSE
##   runs.signal longest.run longest.run.max n.crossings n.crossings.min
## 1       FALSE           3               7           6               3
## 2       FALSE           3               7           6               3
## 3       FALSE           2               7           7               3
## 4       FALSE           3               7           7               3
## 5       FALSE           3               7           5               3
## 6       FALSE           5               7           4               3
## 7       FALSE           3               7           6               3
## 8       FALSE           2               7           5               3
# Run chart with two grouping variables
tccplot <- tcc(n, d, mo, g1 = g1, g2 = g2, data = d)
tccplot

ggplotly(tccplot)
# P chart
tccplot <- tcc(n, d, mo, g1 = g1, g2 = g2, data = d, chart = 'p')
tccplot

ggplotly(tccplot)
# P chart with baseline fixed to the first 12 data points
tccplot <- tcc(n, d, mo, g1 = g1, g2 = g2, data = d, chart = 'p', freeze = 12)
tccplot

ggplotly(tccplot)
# P chart with two breaks and summary output
tccplot <- tcc(n, d, mo, g1 = g1, g2 = g2, data = d, chart = 'p',
    breaks = c(12, 18), print.summary = TRUE)
##    g1 g2 breaks n.obs n.useful        cl       lcl       ucl limits.signal
## 1   a  A      1    12       12 0.5292642 0.3464595 0.7120689         FALSE
## 2   a  A      2     6        6 0.5060241 0.4201229 0.5919253         FALSE
## 3   a  A      3     6        6 0.4749596 0.2179410 0.7319783         FALSE
## 4   a  B      1    12       12 0.4967051 0.3075495 0.6858607         FALSE
## 5   a  B      2     6        6 0.5101010 0.3103471 0.7098549         FALSE
## 6   a  B      3     6        6 0.4967638 0.3097258 0.6838017         FALSE
## 7   b  A      1    12       12 0.5068826 0.4148747 0.5988905         FALSE
## 8   b  A      2     6        6 0.5228426 0.4489229 0.5967624         FALSE
## 9   b  A      3     6        6 0.5155993 0.3869437 0.6442550         FALSE
## 10  b  B      1    12       12 0.5214106 0.2941180 0.7487032         FALSE
## 11  b  B      2     6        6 0.5479930 0.3774447 0.7185413         FALSE
## 12  b  B      3     6        6 0.4845361 0.3251248 0.6439473         FALSE
##    runs.signal longest.run longest.run.max n.crossings n.crossings.min
## 1        FALSE           3               7           6               3
## 2        FALSE           3               6           2               1
## 3        FALSE           2               6           3               1
## 4        FALSE           2               7           7               3
## 5        FALSE           2               6           3               1
## 6        FALSE           1               6           5               1
## 7        FALSE           3               7           5               3
## 8        FALSE           2               6           3               1
## 9        FALSE           3               6           1               1
## 10       FALSE           3               7           6               3
## 11       FALSE           2               6           2               1
## 12       FALSE           2               6           2               1
tccplot

ggplotly(tccplot)
set.seed(1)
# Run chart of 24 samples of a random continuous variable
# with an approximate mean = 12 and standard deviation = 3.
y <- rnorm(24, 12, 3)
tccplot <- tcc(y)
tccplot

ggplotly(tccplot)
# Add subgroup vector (dates) and a target
x <- seq.Date(as.Date('2013-08-04'), by = 'week', length = 24)
tccplot <- tcc(y, x = x, target = 16)
tccplot

ggplotly(tccplot)
# Individuals control chart
tccplot <- tcc(y, x = x, chart = 'i')
tccplot

ggplotly(tccplot)
# Xbar control chart, sample size = 5
y <- rnorm(5 * 24)
x <- rep(x, 5)
tccplot <- tcc(y, x = x, chart = 'xbar')
tccplot

ggplotly(tccplot)
# Create data frame with counts and sample sizes by week
d <- data.frame(week = seq.Date(as.Date('2013-08-04'),
                                by = 'week',
                                length = 36),
                y = c(rbinom(24, 20, 0.5), rbinom(12, 20, 0.8)),
                n = round(rnorm(36, 20, 2)))

# Proportions control chart
tccplot <- tcc(y, n, x = week, data = d[1:24,], chart = 'p')
tccplot

ggplotly(tccplot)
# Introduce change in process performance
tccplot <- tcc(y, n, x = week, data = d, chart = 'p')
tccplot

ggplotly(tccplot)
# Freeze baseline to first 24 samples
tccplot <- tcc(y, n, x = week, data = d, chart = 'p', freeze = 24)
tccplot

ggplotly(tccplot)
# Break control chart before and after change
tccplot <- tcc(y, n, x = week, data = d, chart = 'p', breaks = 24)
tccplot

ggplotly(tccplot)
# Introduce extreme sample value and notes
d$a <- ''
d$a[30] <- 'Extreme value'
d$y[30] <- 1
tccplot <- tcc(y, n, x = week, data = d, chart = 'p',
    breaks = 24,
    notes = a)
tccplot

ggplotly(tccplot)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues
# Exclude value from calculations
d$a[30] <- 'Value excluded from calculations'
tccplot <- tcc(y, n, x = week, data = d, chart = 'p',
    breaks = 24,
    notes = a,
    exclude = 30)
tccplot

ggplotly(tccplot)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues